﻿using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using DevExpress.Data;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraExport;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Export;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraPrinting;

namespace E4RPDevExpress
{
    public class DevExpressHelper
    {
        #region GridView

        public static void ExportToExcel(GridView gridView)
        {
            var filePath = Path.GetTempFileName();
            filePath = Path.ChangeExtension(filePath, ".xls");
            gridView.ExportToXls(filePath, new XlsExportOptions(TextExportMode.Value));
            Process.Start(filePath);
        }

        public static void ColumnsCustomization(GridView gridView)
        {
            gridView.ColumnsCustomization();
        }

        public static void ShowGroupPanel(GridView gridView)
        {
            gridView.OptionsView.ShowGroupPanel = !gridView.OptionsView.ShowGroupPanel;
        }

        public static void ShowPrintPreview(GridView gridView)
        {
            gridView.GridControl.ShowPrintPreview();
        }

        public static void Print(GridView gridView)
        {
            gridView.GridControl.Print();
        }

        public static void CopyData(GridView gridView)
        {
            var stream = new MemoryStream();
            var encoder = Encoding.UTF8;
            var provider = new ExportTxtProvider(stream) { Separator = "\t" };
            var link = (GridViewExportLink)gridView.CreateExportLink(provider);
            link.ExportTo(true);
            Clipboard.SetDataObject(encoder.GetString(stream.GetBuffer()));
            stream.Close();
        }

        public static void SetColumnAsCurrency(GridColumn gridColumn)
        {
            gridColumn.DisplayFormat.FormatString = "c";
            gridColumn.DisplayFormat.FormatType = FormatType.Numeric;
            gridColumn.SummaryItem.DisplayFormat = "{0:c2}";
            gridColumn.SummaryItem.SummaryType = SummaryItemType.Sum;
            gridColumn.SummaryItem.FieldName = gridColumn.FieldName;
        }

        public static void SetColumnAsNumeric(GridColumn gridColumn)
        {
            gridColumn.DisplayFormat.FormatString = "n2";
            gridColumn.DisplayFormat.FormatType = FormatType.Numeric;
        }

        public static void SetColumnAsInteger(GridColumn gridColumn)
        {
            gridColumn.DisplayFormat.FormatString = "n0";
            gridColumn.DisplayFormat.FormatType = FormatType.Numeric;
            gridColumn.SummaryItem.DisplayFormat = "{0:n0}";
            gridColumn.SummaryItem.SummaryType = SummaryItemType.Sum;
            gridColumn.SummaryItem.FieldName = gridColumn.FieldName;
        }

        public static void SetColumnAsPercentage(GridColumn gridColumn)
        {
            gridColumn.DisplayFormat.FormatString = "p2";
            gridColumn.DisplayFormat.FormatType = FormatType.Numeric;
            gridColumn.SummaryItem.DisplayFormat = "{0:p2}";
            gridColumn.SummaryItem.SummaryType = SummaryItemType.Average;
            gridColumn.SummaryItem.FieldName = gridColumn.FieldName;
        }

        public static void ShowGroupSummaryAsCurrency(GridView gridView, List<GridColumn> gridColumns)
        {
            gridView.GroupFooterShowMode = GroupFooterShowMode.VisibleIfExpanded;
            foreach (GridColumn gridColumn in gridView.Columns)
            {
                if (!gridColumns.Contains(gridColumn))
                    continue;
                var item = new GridGroupSummaryItem
                {
                    ShowInGroupColumnFooter = gridColumn,
                    FieldName = gridColumn.FieldName,
                    DisplayFormat = "{0:c2}",
                    SummaryType = SummaryItemType.Sum
                };
                gridView.GroupSummary.Add(item);
            }
        }

        public static void ShowGroupSummaryAsCurrency(GridView gridView)
        {
            gridView.GroupFooterShowMode = GroupFooterShowMode.VisibleIfExpanded;
            foreach (GridColumn column in gridView.Columns)
            {
                if (column.DisplayFormat.FormatType != FormatType.Numeric)
                    continue;
                var item = new GridGroupSummaryItem
                {
                    ShowInGroupColumnFooter = column,
                    FieldName = column.FieldName,
                    DisplayFormat = "{0:c2}",
                    SummaryType = SummaryItemType.Sum
                };
                gridView.GroupSummary.Add(item);
            }
        }

        public static void ShowGroupSummaryAsNumeric(GridView gridView, List<GridColumn> gridColumns)
        {
            gridView.GroupFooterShowMode = GroupFooterShowMode.VisibleIfExpanded;
            foreach (GridColumn gridColumn in gridView.Columns)
            {
                if (!gridColumns.Contains(gridColumn))
                    continue;
                var item = new GridGroupSummaryItem
                {
                    ShowInGroupColumnFooter = gridColumn,
                    FieldName = gridColumn.FieldName,
                    DisplayFormat = "{0:n2}",
                    SummaryType = SummaryItemType.Sum
                };
                gridView.GroupSummary.Add(item);
            }
        }

        public static void ShowGroupSummaryAsInteger(GridView gridView, List<GridColumn> gridColumns)
        {
            gridView.GroupFooterShowMode = GroupFooterShowMode.VisibleIfExpanded;
            foreach (GridColumn gridColumn in gridView.Columns)
            {
                if (!gridColumns.Contains(gridColumn))
                    continue;
                var item = new GridGroupSummaryItem
                {
                    ShowInGroupColumnFooter = gridColumn,
                    FieldName = gridColumn.FieldName,
                    DisplayFormat = "{0:0}",
                    SummaryType = SummaryItemType.Sum
                };
                gridView.GroupSummary.Add(item);
            }
        }

        public static void ShowGroupAverageAsPercentage(GridView gridView, List<GridColumn> gridColumns)
        {
            gridView.GroupFooterShowMode = GroupFooterShowMode.VisibleIfExpanded;
            foreach (GridColumn gridColumn in gridView.Columns)
            {
                if (!gridColumns.Contains(gridColumn))
                    continue;
                var item = new GridGroupSummaryItem
                {
                    ShowInGroupColumnFooter = gridColumn,
                    FieldName = gridColumn.FieldName,
                    DisplayFormat = "{0:p2}",
                    SummaryType = SummaryItemType.Average
                };
                gridView.GroupSummary.Add(item);
            }
        }

        public static void ShowGroupSummaryAsNumeric(GridView gridView)
        {
            gridView.GroupFooterShowMode = GroupFooterShowMode.VisibleIfExpanded;
            foreach (GridColumn column in gridView.Columns)
            {
                if (column.DisplayFormat.FormatType != FormatType.Numeric)
                    continue;
                var item = new GridGroupSummaryItem
                {
                    ShowInGroupColumnFooter = column,
                    FieldName = column.FieldName,
                    DisplayFormat = "{0:n2}",
                    SummaryType = SummaryItemType.Sum
                };
                gridView.GroupSummary.Add(item);
            }
        }

        public static void ShowSummaries(GridView gridView)
        {
            gridView.OptionsView.ShowFooter = !gridView.OptionsView.ShowFooter;
        }

        public static object SaveLayoutToStream(GridView gridView)
        {
            using (var stream = new MemoryStream())
            {
                gridView.SaveLayoutToStream(stream);
                stream.Close();
                return stream.GetBuffer();
            }
        }

        public static void RestoreLayoutFromStream(object value, GridView gridView)
        {
            if (!(value is byte[]))
                return;
            if (((byte[])value).Length == 0)
                return;
            using (var stream = new MemoryStream((byte[])value))
            {
                gridView.RestoreLayoutFromStream(stream);
                stream.Close();
            }
        }

        public static string SaveLayoutToXml(GridView gridView)
        {
            var xmlFile = Path.GetTempFileName();
            xmlFile = Path.ChangeExtension(xmlFile, ".xml");
            gridView.SaveLayoutToXml(xmlFile);
            var xmlDocument = new XmlDocument();
            xmlDocument.Load(xmlFile);
            return xmlDocument.InnerXml;
        }

        public static void RestoreLayoutFromXml(GridView gridView, string xmlLayout)
        {
            var xmlFile = Path.GetTempFileName();
            xmlFile = Path.ChangeExtension(xmlFile, ".xml");
            var xmlDocument = new XmlDocument { InnerXml = xmlLayout };
            xmlDocument.Save(xmlFile);
            gridView.RestoreLayoutFromXml(xmlFile);
        }

        public static void SetColumnAsNumericAndPaint(GridColumn column)
        {
            column.View.BeginInit();
            column.View.GridControl.BeginInit();
            if ((column.View as GridView) != null)
                (column.View as GridView).OptionsView.ShowFooter = true;
            column.DisplayFormat.FormatString = "n2";
            column.DisplayFormat.FormatType = FormatType.Numeric;
            column.SummaryItem.DisplayFormat = "{0:n2}";
            column.SummaryItem.SummaryType = SummaryItemType.Sum;
            column.SummaryItem.FieldName = column.FieldName;

            var styleFormatCondition = new StyleFormatCondition();
            styleFormatCondition.Appearance.ForeColor = Color.Red;
            styleFormatCondition.Appearance.Options.UseForeColor = true;
            styleFormatCondition.Condition = FormatConditionEnum.Less;
            styleFormatCondition.Value1 = 0;
            (column.View as GridView).FormatConditions.AddRange(new[] { styleFormatCondition });
            styleFormatCondition.Column = column;
            column.View.EndInit();
            column.View.GridControl.EndInit();
        }

        #endregion

        #region CalcEdit

        public static void SetAsCurrency(CalcEdit calcEdit)
        {
            calcEdit.Properties.EditMask = "c2";
            calcEdit.Properties.DisplayFormat.FormatString = "c2";
        }

        #endregion
    }
}
